#include "Unit.h"
#include "Texture.h"

CUnit::CUnit()
: id(0)
, team(0)
, health(0)
, damage(0)
, detRadius(0)
, speed(0)
, atkrate(0)
, size(0)
, active(false)
, toggle(false)
, pos(Vector3D(0, 0, 0))
, color(Vector3D(0, 0, 0))
, CurrentMsg(NULL)
{
	mb = new MessageBoard();
}

CUnit::~CUnit()
{

}

int CUnit::GetID(void)
{
	return id;
}

int CUnit::GetTeam(void)
{
	return team;
}

int CUnit::GetHealth(void)
{
	return health;
}

int CUnit::GetMaxHealth(void)
{
	return maxHealth;
}

int CUnit::GetDamage(void)
{
	return damage;
}

int CUnit::GetDetRadius(void)
{
	return detRadius;
}

int CUnit::GetAtkRadius(void)
{
	return atkRadius;
}

int CUnit::GetSize(void)
{
	return size;
}

float CUnit::GetSpeed(void)
{
	return speed;
}

float CUnit::GetAttackRate(void)
{
	return atkrate;
}

bool CUnit::GetActive(void)
{
	return active;
}

bool CUnit::GetToggle(void)
{
	return toggle;
}

Vector3D CUnit::GetPos(void)
{
	return pos;
}

Vector3D CUnit::GetColor(void)
{
	return color;
}

string CUnit::RcvMessage(MessageBoard *mb)
{
	//return mb->RcvMessage();
	return "a";
}

string CUnit::MsgFrom(MessageBoard *mb)
{
	//return mb->GetFrom();
	return "a";
}

string CUnit::MsgTo(MessageBoard *mb)
{
	//return mb->GetTo();
	return "a";
}

void CUnit::SetID(int id)
{
	this->id = id;
}

void CUnit::SetTeam(int team)
{
	this->team = team;
}

void CUnit::SetHealth(int health)
{
	this->health = health;
}

void CUnit::SetMaxHealth(int maxHealth)
{
	this->maxHealth = maxHealth;
}

void CUnit::SetDamage(int damage)
{
	this->damage = damage;
}

void CUnit::SetDetRadius(int detRadius)
{
	this->detRadius = detRadius;
}

void CUnit::SetAtkRadius(int atkRadius)
{
	this->atkRadius = atkRadius;
}

void CUnit::SetSize(int size)
{
	this->size = size;
}

void CUnit::SetSpeed(float speed)
{
	this->speed = speed;
}

void CUnit::SetAttackRate(float atkrate)
{
	this->atkrate = atkrate;
}

void CUnit::SetActive(bool active)
{
	this->active = active;
}

void CUnit::SetToggle(bool toggle)
{
	this->toggle = toggle;
}

void CUnit::SetPos(Vector3D pos)
{
	this->pos = pos;
}

void CUnit::SetColor(Vector3D color)
{
	this->color = color;
}

void CUnit::SetMessage(MessageBoard *mb, string from, string to, string msg)
{
	//mb->SetMessage(from, to, msg);
}

void CUnit::RenderHealthBar(void)
{
	glDisable(GL_BLEND);
	glPushMatrix();
		glColor3f(1.0f, 1.0f, 1.0f);
		glTranslatef(pos.x - 50, pos.y - 35, 0);
		glBegin(GL_LINE_LOOP);
			glVertex2f(0-1, 0-1);
			glVertex2f(maxHealth+1, 0-1);
			glVertex2f(maxHealth+1, 10+1);
			glVertex2f(0-1, 10+1);
		glEnd();
		glColor3f(1.0f, 0.0f, 0.0f);
		glBegin(GL_QUADS);
			glVertex2f(0, 0);
			glVertex2f(maxHealth, 0);
			glVertex2f(maxHealth, 10);
			glVertex2f(0, 10);
		glEnd();
		glPushMatrix();
			glColor3f(0.0f, 1.0f, 0.0f);
			glBegin(GL_QUADS);
				glVertex2f(0, 0);
				glVertex2f(health, 0);
				glVertex2f(health, 10);
				glVertex2f(0, 10);
			glEnd();
		glPopMatrix();
	glPopMatrix();
	glEnable(GL_BLEND);
}

bool CUnit::LoadTGA(Texture *texture, char *filename)			// Loads A TGA File Into Memory
{
	GLubyte		TGAheader[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };	// Uncompressed TGA Header
	GLubyte		TGAcompare[12];								// Used To Compare TGA Header
	GLubyte		header[6];									// First 6 Useful Bytes From The Header
	GLuint		bytesPerPixel;								// Holds Number Of Bytes Per Pixel Used In The TGA File
	GLuint		imageSize;									// Used To Store The Image Size When Setting Aside Ram
	GLuint		temp;										// Temporary Variable
	GLuint		type = GL_RGBA;								// Set The Default GL Mode To RBGA (32 BPP)

	FILE *file = fopen(filename, "rb");						// Open The TGA File

	if (file == NULL ||										// Does File Even Exist?
		fread(TGAcompare, 1, sizeof(TGAcompare), file) != sizeof(TGAcompare) ||	// Are There 12 Bytes To Read?
		memcmp(TGAheader, TGAcompare, sizeof(TGAheader)) != 0 ||	// Does The Header Match What We Want?
		fread(header, 1, sizeof(header), file) != sizeof(header))				// If So Read Next 6 Header Bytes
	{
		if (file == NULL)									// Did The File Even Exist? *Added Jim Strong*
			return false;									// Return False
		else
		{
			fclose(file);									// If Anything Failed, Close The File
			return false;									// Return False
		}
	}

	texture->width = header[1] * 256 + header[0];			// Determine The TGA Width	(highbyte*256+lowbyte)
	texture->height = header[3] * 256 + header[2];			// Determine The TGA Height	(highbyte*256+lowbyte)

	if (texture->width <= 0 ||								// Is The Width Less Than Or Equal To Zero
		texture->height <= 0 ||								// Is The Height Less Than Or Equal To Zero
		(header[4] != 24 && header[4] != 32))					// Is The TGA 24 or 32 Bit?
	{
		fclose(file);										// If Anything Failed, Close The File
		return false;										// Return False
	}

	texture->bpp = header[4];							// Grab The TGA's Bits Per Pixel (24 or 32)
	bytesPerPixel = texture->bpp / 8;						// Divide By 8 To Get The Bytes Per Pixel
	imageSize = texture->width*texture->height*bytesPerPixel;	// Calculate The Memory Required For The TGA Data

	texture->imageData = (GLubyte *)malloc(imageSize);		// Reserve Memory To Hold The TGA Data

	if (texture->imageData == NULL ||							// Does The Storage Memory Exist?
		fread(texture->imageData, 1, imageSize, file) != imageSize)	// Does The Image Size Match The Memory Reserved?
	{
		if (texture->imageData != NULL)						// Was Image Data Loaded
			free(texture->imageData);						// If So, Release The Image Data

		fclose(file);										// Close The File
		return false;										// Return False
	}

	for (GLuint i = 0; i < int(imageSize); i += bytesPerPixel)		// Loop Through The Image Data
	{														// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
		temp = texture->imageData[i];							// Temporarily Store The Value At Image Data 'i'
		texture->imageData[i] = texture->imageData[i + 2];	// Set The 1st Byte To The Value Of The 3rd Byte
		texture->imageData[i + 2] = temp;					// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
	}

	fclose(file);											// Close The File

	// Build A Texture From The Data
	glGenTextures(1, &texture[0].texID);					// Generate OpenGL texture IDs

	glBindTexture(GL_TEXTURE_2D, texture[0].texID);			// Bind Our Texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Filtered
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Filtered

	if (texture[0].bpp == 24)									// Was The TGA 24 Bits
	{
		type = GL_RGB;										// If So Set The 'type' To GL_RGB
	}

	glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);

	return true;											// Texture Building Went Ok, Return True
}

void CUnit::CheckMessage(std::vector<MessageBoard*> &Msgb)
{

}